package ide.ccf.ccproject.pages.embedded;

import java.io.IOException;
import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

/**
 * 作用域列表
 *
 */
public class ScopeList extends Composite
{
    long handleWidget;
    @SuppressWarnings("rawtypes")
	static Hashtable table = new Hashtable();
    static 
    {
        try 
        {
            System.loadLibrary("ccfprojectfileparser");
        } 
        catch (UnsatisfiedLinkError ex) 
        { 
        	ex.printStackTrace(); 
        }
    }

    /**
     * 构造器
     * @param parent
     * @param style
     */
    @SuppressWarnings("unchecked")
	public ScopeList(Composite parent, int style)
    {
        super (parent, style);
        handleWidget = createControl(handle, embeddedHandle);
        
        if (handleWidget == 0) 
        	SWT.error (SWT.ERROR_NO_HANDLES);
        
        table.put(new Long(handleWidget), this);
        
        setFont(getFont());
    
        addDisposeListener(new DisposeListener() 
        {
            public void widgetDisposed(DisposeEvent e) 
            {
                ScopeList.this.widgetDisposed(e);
            }
        });

        addControlListener(new ControlAdapter() 
        {
            public void controlResized(ControlEvent e) 
            {
                ScopeList.this.controlResized(e);
            }
        });
    }

    /**
     * 控件资源释放
     * @param e
     */
    public void widgetDisposed (DisposeEvent e)
    {
        table.remove(new Long(handleWidget));
        disposeControl(handleWidget);
        handleWidget = 0;
    }
  
    /**
     * 重绘大小
     * @param e
     */
    public void controlResized(ControlEvent e)
    {
        Rectangle rect = getClientArea();
        resizeControl(handleWidget, rect.x, rect.y, rect.width, rect.height);
    }

    /**
     * 计算大小
     */
    public Point computeSize(int wHint, int hHint, boolean changed)
    {
        checkWidget();
        int [] result = new int[2];
        computeSize(handleWidget, result);
        
        if (wHint != SWT.DEFAULT) 
        	result[0] = wHint;
        
        if (hHint != SWT.DEFAULT) 
        	result[1] = hHint;
        
        int border = getBorderWidth();
        
        return new Point(result[0] + border * 2, result[1] + border * 2);
    }
    
    /**
     * 设置字体
     */
    public void setFont(Font font)
    {
        super.setFont(font);
        
        if (font == null)
            return;
        
        FontData[] fntlist = font.getFontData();
        setFont (handleWidget, fntlist[0].getName(), fntlist[0].getHeight());
    }
    
    /**
     * 显示对话框
     * @param projectFileName
     * @param contents
     * @param enabled
     */
    public void showModel(String projectFileName, String contents, boolean enabled)
    {
        checkWidget();
        showModel(handleWidget, projectFileName, contents, enabled);
    }

    /**
     * 显示
     */
    public void selectFirstVariable()
    {
        checkWidget();
        selectFirstVariable(handleWidget);
    }

    /**
     * 搜索
     * @param projectFileName
     * @param contents
     * @return
     */
    public boolean search(String projectFileName, String contents)
    {
        checkWidget();
        return search(handleWidget, projectFileName, contents);
    }

    /**
     * 是否变化
     * @param projectFileName
     * @return
     */
    public boolean isChanged(String projectFileName)
    {
        checkWidget();
        return isChanged(handleWidget, projectFileName);
    }

    /**
     * 删除文件
     * @param projectFileName
     * @return
     */
    public String removeFiles(String projectFileName)
    {
        checkWidget();
        return removeFiles(handleWidget, projectFileName);
    }

    /**
     * 添加文件
     * @param projectFileName
     * @return
     */
    public String addFiles(String projectFileName)
    {
        checkWidget();
        return addFiles(handleWidget, projectFileName);
    }

    /**
     * 添加文件
     * @param file
     * @param var
     */
    public void addFile(String file, String var)
    {
        checkWidget();
        addFile(handleWidget, file, var);
    }

    static final native long createControl(long phandle, long socketWin);
    static final native void computeSize(long handle, int [] result);
    static final native void disposeControl(long handle);
    static final native void resizeControl(long handle, int x, int y, int width, int height);
    static final native void setFont(long handle, String family, int size);
    static final native void showModel(long handle, String projectFileName, String contents, boolean enabled);
    static final native void selectFirstVariable(long handle);
    static final native boolean search(long handle, String projectFileName, String contents);
    static final native boolean isChanged(long handle, String projectFileName);
    static final native String removeFiles(long handle, String projectFileName);
    static final native String addFiles(long handle, String projectFileName);
    static final native void addFile(long handle, String file, String var);
}
